<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-forms gv-forms-fluid" layout="column">
  <application-header application="$ctrl.application"></application-header>
  <div class="gv-form">
    <h2>Subscriptions</h2>
    <div class="gv-form-content" layout="column">
      <form ng-submit="$ctrl.search()" name="$ctrl.subscriptionsFiltersForm">
        <div layout-gt-sm="row">
          <md-input-container class="md-block" flex-gt-sm flex="40">
            <label>Api</label>
            <md-select ng-model="$ctrl.query.apis" placeholder="Apis" multiple>
              <md-option ng-value="subscriber.id" ng-repeat="subscriber in $ctrl.subscribers track by subscriber.id"
                >{{ subscriber.name }} ()
              </md-option>
            </md-select>
          </md-input-container>
          <md-input-container class="md-block" flex-gt-sm flex="40">
            <label>Status</label>
            <md-select ng-model="$ctrl.query.status" placeholder="Status" multiple>
              <md-option ng-value="key" ng-repeat="(key, value) in $ctrl.status">{{ value }}</md-option>
            </md-select>
          </md-input-container>
          <md-input-container class="md-block" flex-gt-sm flex="20">
            <label>API key</label>
            <input ng-model="$ctrl.query.api_key" />
          </md-input-container>
          <div>
            <md-button type="submit" class="md-raised md-primary"> Search </md-button>
            <md-button type="button" class="md-raised" ng-click="$ctrl.clearFilters()" ng-disabled="!$ctrl.hasFilter()"> Clear </md-button>
          </div>
        </div>
      </form>

      <md-table-container ng-if="$ctrl.subscriptions.data.length > 0">
        <table md-table class="gv-table-dense">
          <thead md-head>
            <tr md-row>
              <th md-column>API</th>
              <th md-column>Plan</th>
              <th md-column>Created at</th>
              <th md-column>Processed at</th>
              <th md-column>Start at</th>
              <th md-column>End at</th>
              <th md-column>Status</th>
            </tr>
          </thead>
          <tbody md-body>
            <tr md-row ng-repeat="subscription in $ctrl.subscriptions.data track by subscription.id">
              <td md-cell>
                <a
                  style="font-weight: bold"
                  ui-sref="management.applications.application.subscriptions.subscription({subscriptionId: subscription.id,
            page: $ctrl.query.page,
            size: $ctrl.query.size,
            status: $ctrl.query.status,
            api: $ctrl.query.apis,
            api_key: $ctrl.query.api_key})"
                >
                  {{$ctrl.subscriptions.metadata[subscription.api].name}}
                </a>
              </td>
              <td md-cell>{{$ctrl.subscriptions.metadata[subscription.plan].name}}</td>
              <td md-cell>{{subscription.created_at | date:'yyyy-MM-dd HH:mm:ss'}}</td>
              <td md-cell>{{subscription.processed_at | date:'yyyy-MM-dd HH:mm:ss'}}</td>
              <td md-cell>{{subscription.starting_at || '-' | date:'yyyy-MM-dd HH:mm:ss'}}</td>
              <td md-cell>{{subscription.ending_at || '-' | date:'yyyy-MM-dd HH:mm:ss'}}</td>
              <td md-cell style="text-transform: capitalize">
                <md-tooltip ng-if="subscription.reason" md-direction="left">{{subscription.reason}}</md-tooltip>
                {{subscription.status}}
              </td>
            </tr>
          </tbody>
        </table>
      </md-table-container>

      <md-table-pagination
        ng-if="$ctrl.subscriptions.data.length > 0"
        md-limit="$ctrl.subscriptions.page.per_page"
        md-limit-options="[10, 25, 50, 75, 100]"
        md-page="$ctrl.subscriptions.page.current"
        md-total="{{$ctrl.subscriptions.page.total_elements}}"
        md-on-paginate="$ctrl.onPaginate"
        md-page-select
      >
      </md-table-pagination>

      <gravitee-empty-state
        ng-if="$ctrl.subscriptions.data.length === 0"
        icon="vpn_key"
        model="Subscription"
        message="Application's subscriptions will appear here"
      ></gravitee-empty-state>
      <md-button
        ng-if="$ctrl.subscriptions.data.length === 0"
        class="md-raised"
        style="width: 200px; margin: 0 auto"
        ui-sref="management.applications.application.subscriptions.subscribe"
        >Start playing with APIs</md-button
      >
    </div>
  </div>
</div>
